﻿@model Contoso_University.Models.InstructorIndexData

@{
    ViewData["Title"] = "讲师管理";
}

<h1>讲师管理</h1>

<p>
    <a asp-action="Create" class="btn btn-primary">新建讲师</a>
</p>

<div class="row">
    <div class="col-md-8">
        <table class="table-striped table">
            <thead>
                <tr>
                    <th>姓</th>
                    <th>名</th>
                    <th>聘用日期</th>
                    <th>办公室</th>
                    <th>课程</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.Instructors)
                {
                    string selectedRow = "";
                    if (item.ID == (int?)ViewData["InstructorID"])
                    {
                        selectedRow = "table-success";
                    }
                    <tr class="@selectedRow">
                        <td>
                            @Html.DisplayFor(modelItem => item.LastName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.FirstName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.HireDate)
                        </td>
                        <td>
                            @if (item.CourseAssignments != null)
                            {
                                @item.CourseAssignments.Count
                            }
                        </td>
                        <td>
                            @{
                                foreach (var courseAssignment in item.CourseAssignments)
                                {
                                    if (courseAssignment.Course != null)
                                    {
                                        @courseAssignment.Course.CourseID @:  @courseAssignment.Course.Title
                                        <form asp-action="RemoveCourse" method="post" style="display:inline;">
                                            <input type="hidden" name="instructorID" value="@item.ID" />
                                            <input type="hidden" name="courseID" value="@courseAssignment.Course.CourseID" />
                                            <button type="submit" class="btn btn-sm btn-link text-danger ms-2 p-0">移除</button>
                                        </form>
                                        <br />
                                    }
                                }
                            }
                            <form asp-action="AssignCourse" method="post" class="mt-2">
                                <input type="hidden" name="instructorID" value="@item.ID" />
                                <div class="input-group input-group-sm">
                                    <select name="courseID" class="form-select form-select-sm">
                                        @foreach (var course in ViewBag.AllCourses as List<Contoso_University.Models.Course>)
                                        {
                                            <option value="@course.CourseID">@course.Title</option>
                                        }
                                    </select>
                                    <button type="submit" class="btn btn-success btn-sm">分配课程</button>
                                </div>
                            </form>
                        </td>
                        <td>
                            <!--<a asp-action="Index" asp-route-id="@item.ID" class="btn btn-sm btn-outline-info">选择</a>-->
                            <a asp-action="Edit" asp-route-id="@item.ID" class="btn btn-sm btn-outline-primary">编辑</a>
                            <!--<a asp-action="Details" asp-route-id="@item.ID" class="btn btn-sm btn-outline-secondary">详情</a>-->
                            <a asp-action="Delete" asp-route-id="@item.ID" class="btn btn-sm btn-outline-danger">删除</a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="col-md-4">
        @if (Model.Courses != null)
        {
            <h3>选中讲师教授的课程</h3>
            <table class="table-sm table">
                <tr>
                    <th>编号</th>
                    <th>课程名称</th>
                    <th>院系</th>
                </tr>

                @foreach (var item in Model.Courses)
                {
                    string selectedRow = "";
                    if (item.CourseID == (int?)ViewData["CourseID"])
                    {
                        selectedRow = "table-success";
                    }
                    <tr class="@selectedRow">
                        <td>
                            @item.CourseID
                        </td>
                        <td>
                            @item.Title
                        </td>
                        <td>
                            @(item.Department != null ? item.Department.Name : "")
                        </td>
                    </tr>
                }
            </table>
        }

        @if (Model.Enrollments != null)
        {
            <h3>
                选中课程的学生名单
            </h3>
            <table class="table-sm table">
                <tr>
                    <th>姓名</th>
                    <th>成绩</th>
                </tr>
                @foreach (var item in Model.Enrollments)
                {
                    <tr>
                        <td>
                            @(item.Student != null ? item.Student.FullName : "")
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Grade)
                        </td>
                    </tr>
                }
            </table>
        }
    </div>
</div>

